package com.google.code.swingonfire.utility.date;

import java.awt.geom.Rectangle2D;
import java.text.DateFormatSymbols;
import java.util.Calendar;

class DateRendererMonthPart extends AbstractDateRenderPart
{

    private static final String[] NUMERIC_VALUES = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
        "12"};

    public DateRendererMonthPart(DateRenderer info)
    {
        super(info);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Rectangle2D computeMaximumBounds(DateRendererContext context, DateFormatStyle style)
    {
        String[] values = null;

        switch (style)
        {
            case LONG:
                values = DateFormatSymbols.getInstance(context.getLocale()).getMonths();
                break;

            case MEDIUM:
                values = DateFormatSymbols.getInstance(context.getLocale()).getShortMonths();
                break;

            case SHORT:
                values = NUMERIC_VALUES;
                break;
        }

        return findMaximumBounds(context, values);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getValue(DateRendererContext context, DateFormatStyle style, Calendar calendar)
    {
        int month = calendar.get(Calendar.MONTH);

        switch (style)
        {
            case LONG:
                return DateFormatSymbols.getInstance(context.getLocale()).getMonths()[month];

            case MEDIUM:
                return DateFormatSymbols.getInstance(context.getLocale()).getShortMonths()[month];

            case SHORT:
                return String.format("%02d", month + 1);
        }

        throw new IllegalArgumentException("Unsupported style: " + style);
    }

}
